package com.awakening.action;

import com.awakening.service.ThumbnailService;
import com.awakening.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * @Author:AwakeningCode
 * @Date: Created in 12:17 2018\1\23 0023
 */
@Controller
@RequestMapping("/")
public class ThumbnailAction {

    private UploadService uploadService;
    private ThumbnailService thumbnailService;

    @RequestMapping(value = "/thumbnail",method = RequestMethod.POST)
    public ModelAndView thumbnail(@RequestParam("image") CommonsMultipartFile file, HttpSession session)throws Exception{
        String uploadPath = "/images";
        //获取绝对路径
        String realUploadPath = session.getServletContext().getRealPath(uploadPath);

        String imageUrl = uploadService.uploadImage(file,uploadPath,realUploadPath);
        String thumImageUrl = thumbnailService.thumbnail(file,uploadPath,realUploadPath);

        ModelAndView ret = new ModelAndView();
        ret.addObject("imageUrl",imageUrl);
        ret.addObject("thumImageUrl",thumImageUrl);

        ret.setViewName("thumbnail");

        return ret;
    }

    @Autowired
    public void setUploadService(UploadService uploadService) {
        this.uploadService = uploadService;
    }

    @Autowired
    public void setThumbnailService(ThumbnailService thumbnailService) {
        this.thumbnailService = thumbnailService;
    }
}
